using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SMS.Framework;
using System.Xml;
using System.Xml.XPath;

namespace SMS.TrayWindow
{
    public partial class TrayForm : Form
    {
        public TrayForm()
        {
            InitializeComponent();
            InitializeTrayMenu();
        }

        private ContextMenu cm = new ContextMenu();
        private void InitializeTrayMenu()
        {
            this.NotifyIcon.ContextMenu = cm;
            this.NotifyIcon.Click += new EventHandler(NotifyIcon_Click);
        }

        void NotifyIcon_Click(object sender, EventArgs e)
        {
            //throw new Exception("The method or operation is not implemented.");
            Context.Current.ExecuteApplicationCommand(this,"ShowMainForm", null);
        }

        public void AddMenus()
        {
            cm.MenuItems.Clear();
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(@"./Settings/TrayMenus.xml");
                foreach (XmlNode node in doc.GetElementsByTagName("MenuItem"))
                {
                    MenuItem mi = new MenuItem(node.Attributes["text"].Value);
                    mi.Name = node.Attributes["name"].Value;
                    mi.Tag = node.Attributes["action"].Value;
                    if (Context.Current.User.IsInRole(node.Attributes["role"].Value.ToLower()))
                    {
                        mi.Enabled = true;
                    }
                    else
                    {
                        mi.Enabled = false;
                    }
                    mi.Click += new EventHandler(mi_Click);
                    cm.MenuItems.Add(mi);
                }
            }
            catch
            { }
        }

        void mi_Click(object sender, EventArgs e)
        {
            Context.Current.ExecuteApplicationCommand(this,((MenuItem)sender).Tag.ToString(),null);
        }


        private void TrayForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing) e.Cancel = true;
        }

        private void TrayForm_Activated(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void TrayForm_Load(object sender, EventArgs e)
        {
        }

    }
}